<?php
/**
* 数据库切片的基本类
*/
class Par extends CActiveRecord
{
 

    protected static $tableSuffix = '';
    protected static $tablename = 'pars';
    
    
    /**
     * Constructor.
     * @param array $entry=array($key=>$value) key is the partition field name.
     * @param array $opts some optitions: hash_num=>the hash num default 10.
     */
    public function __construct($opts = ''){
        if($opts === null){
            return;
        }
        if (strlen($opts) < 5) {
            static::$tableSuffix = $opts;
        }else{
            $this->parseParams($opts);
        }
        
        $tablename = $this->tableName();
        //$this->refreshMetaData();
        //var_dump($this->getMetaData()->tableSchema->name);
        $this->getMetaData()->tableSchema->name = $tablename;
        $this->getMetaData()->tableSchema->rawName = $tablename;

        parent::__construct();
    }

    public static function model($opts = array(), $className=__CLASS__)
    {
        $model = new $className($opts);
        return $model;
    }

    public function tableName()
    {
        return static::$tablename.static::$tableSuffix;
    }

    protected function parseParams($opts){
        static::$tableSuffix = substr($opts, strlen($opts)-2 );
    }

    
}